package com.zmn.oms.task.job.master;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.base.common.data.common.dio.company.CompanyDIO;
import com.zmn.base.common.data.common.dro.company.CompanyDRO;
import com.zmn.base.common.data.dubbo.interfaces.company.CompanyListRemoteService;
import com.zmn.base.plat.engine.common.dto.BasePageDRO;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.business.interfaces.master.MasterQuotationBRateService;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.base.plat.engine.common.constant.CompanyConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ：SongWeidong
 * @date ：2021/3/2 10:15
 * @description：师傅计价器使用率统计
 */
@Slf4j
@LTS
public class MasterQuotationRateJob {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private CompanyListRemoteService companyListRemoteService;
    @Resource
    private TaskManager taskManager;
    @Resource
    private MasterQuotationBRateService masterQuotationBRateService;


    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_MASTER_QUOTATION_RATE_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_MASTER_QUOTATION_RATE_JOB);
        job.setCronExpression("0 0 2 * * ?");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms_master_quotation_rate# job submit fail...");
        }
    }

    /**
     * 每晚统计全国及子公司工程师计价器使用率并放入缓存（每晚两点统计）
     * @param job
     * @return Result
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_MASTER_QUOTATION_RATE_JOB)
    public Result runQuotationRate(Job job) {

        // 查询zmn平台子公司列表
        CompanyDIO baseCompanyDIO = new CompanyDIO();
        baseCompanyDIO.setType(CompanyConsts.COMPANY_TYPE_SUBSIDIARY);
        baseCompanyDIO.setChildType(CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT);
        baseCompanyDIO.setPlat(GlobalConsts.PLAT_MARK_ZMN);
        ResponseDTO<AMISListData<CompanyDRO>> listResponseDTO = companyListRemoteService.listPageCompanyDROByQuery(baseCompanyDIO);

        if (listResponseDTO.isSuccess() && Objects.nonNull(listResponseDTO.getData()) && CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData().getItems())) {
            List<CompanyDRO> data = listResponseDTO.getData().getItems();
            List<Integer> collect = data.stream().map(CompanyDRO::getOrgThrId).collect(Collectors.toList());
            collect.forEach(e -> {
                // 缓存公司数据
                masterQuotationBRateService.saveCompanyQuotationRate(e);
            });
        }
        // 缓存全国数据
        masterQuotationBRateService.saveAllQuotationRate();

        log.info("#oms_master_quotation_rate# 工程师计价器使用率统计任务执行成功 ！");
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

}
